package org.squirrel.modules.bigscreen.controller;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.dto.KeyValue;
import org.squirrel.modules.bigscreen.entity.GaeaDict;
import org.squirrel.modules.bigscreen.service.GaeaDictItemService;
import org.squirrel.modules.bigscreen.service.GaeaDictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 字典
 * 
 * @author leixiangjian
 *
 */
@Slf4j
@RestController
@RequestMapping("/gaeaDict")
@Api(value = "/gaeaDict", tags = "字典项")
public class GaeaDictController extends SquirrelController<GaeaDict, GaeaDictService> {

	@Autowired
	private GaeaDictService gaeaDictService;

	@Autowired
	private GaeaDictItemService gaeaDictItemService;

	/**
	 * 刷新指定字典项
	 * 
	 * @return
	 */
	@ApiOperation(value = "刷新指定字典项", notes = "刷新指定字典项")
	@PostMapping("/freshDict")
	public Result<?> refreshDict(@RequestBody List<String> dictCodes) {
		try {
			// 刷新
			gaeaDictService.refreshCache(dictCodes);
			return Result.ok("刷新指定字典项成功");
		} catch (Exception e) {
			log.error("刷新指定字典项异常", e);
			return Result.error("刷新指定字典项失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 下拉菜单
	 * 
	 * @return
	 */
	@ApiOperation(value = "下拉菜单", notes = "下拉菜单")
	@GetMapping("/select/{dictCode}")
	public Result<?> select(@PathVariable("dictCode") String dictName) {
		try {
			Locale locale = LocaleContextHolder.getLocale();
			// 语言
			String language = locale.getLanguage();
	
			List<KeyValue> keyValues = gaeaDictService.select(dictName, language);
			return Result.OK("下拉菜单成功",keyValues);
		} catch (Exception e) {
			log.error("下拉菜单异常", e);
			return Result.error("下拉菜单失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 指定语言的字典项
	 * 
	 * @return
	 */
	@ApiOperation(value = "指定语言的字典项", notes = "指定语言的字典项")
	@GetMapping("/map/{dictCode}")
	public Result<?> dictItemByLang(@PathVariable("dictCode") String dictCode) {
		try {
			Map<String, String> itemMap = gaeaDictItemService.getItemMap(dictCode);
			
			return Result.OK("指定语言的字典项成功",itemMap);
		} catch (Exception e) {
			log.error("指定语言的字典项异常", e);
			return Result.error("指定语言的字典项失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 下拉菜单
	 * 
	 * @return
	 */
	@ApiOperation(value = "下拉菜单", notes = "下拉菜单")
	@GetMapping("/selectAll/{project}")
	public Result<?> selectTypecodes(@PathVariable String project) {
		try {
			Locale locale = LocaleContextHolder.getLocale();
			// 语言
			String language = locale.getLanguage();
	
			Collection<KeyValue> keyValues = gaeaDictService.selectTypeCode(project, language);
			return Result.OK("下拉菜单",keyValues);
		} catch (Exception e) {
			log.error("下拉菜单异常", e);
			return Result.error("下拉菜单失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 获取所有字典
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有字典", notes = "获取所有字典")
	@GetMapping("/all")
	public Result<?> all() {
		try {
			Locale locale = LocaleContextHolder.getLocale();
			// 语言
			String language = locale.getLanguage();
	
			Map<String, List<KeyValue>> all = gaeaDictService.all(language);
			return Result.OK("获取所有字典",all);
		}catch(Exception e) {
			log.error("获取所有字典异常", e);
			return Result.error("获取所有字典失败,原因:" + e.getMessage());
		}
	}
}
